﻿function XmlBd(xmlFn, templFn)
{
    this.getQuestionSet = function(setName)
    {
        var qset = this.root.selectNodes('set[@name="' + setName + '"]')
        if (qset.length != 1)
        {
            alert('Wrong question set name.')
            return null
        }
        return new QuestionsSet(qset[0], this.qtempl)
    }
    
    this.xml = new ActiveXObject('Msxml2.DOMDocument')
    this.xml.async = false
    this.ok = this.xml.load(xmlFn)
    if (!this.ok) { return }
    this.root = this.xml.documentElement
    
    this.qtempl = new ActiveXObject('Msxml2.DOMDocument')
    this.qtempl.async = false
    this.ok = this.qtempl.load(templFn)
    if (!this.ok) { return }
}

function QuestionsSet(xmlNode, qtempl)
{
    this.getThemeNames = function()
    {
        var names = [this.themeNl.length]
        for (var i = 0; i < this.themeNl.length; i++)
        {
            names[i] = this.themeNl[i].getAttribute('name')
        }
        return names
    }
    
    this.getCosts = function()
    {
        var costs = this.xmlnode.getAttribute('costs').split(' ')
        for (var i = 0; i < costs.length; i++)
        {
            costs[i] = parseInt(costs[i])
        }
        return costs
    }
    
    this.getQuestionHtml = function(theme, cost)
    {
        var query = 'theme[@name="' + theme + '"]/question[cost=' + cost + ']'
        var question = this.xmlnode.selectNodes(query)[0]
        return (question.transformNode(this.qtempl))
    }
    
    this.xmlnode = xmlNode
    this.themeNl = this.xmlnode.selectNodes('theme')
    this.themeNames = this.getThemeNames()
    this.costs = this.getCosts()
    this.qtempl = qtempl
}